package com.ezjie.ielts.db;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ezjie.ielts.core.helper.AppDataBaseHelper;
import com.ezjie.ielts.model.ReadClassifyDetail;
import com.ezjie.ielts.model.SubjectDetail;

public class ReadClassifyInfoDB {
	private static AppDataBaseHelper dbHelper;
	private SQLiteDatabase db;

	public ReadClassifyInfoDB(Context context) {
		dbHelper = AppDataBaseHelper.getInstance(context);
	}

	// 查找阅读题型分类
	public synchronized ReadClassifyDetail query() {
		db = dbHelper.getReadableDatabase();

		ReadClassifyDetail info = new ReadClassifyDetail();

		String sql = "select * from " + AppDataBaseHelper.READ_CLASSIFY;
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			info.cn = cursor.getString(1);
			info.en = cursor.getString(2);
			info.total = cursor.getString(3);
			info.position = cursor.getString(4);
			info.answer_num = cursor.getString(5);
			info.right_num = cursor.getString(6);
		}
		cursor.close();
		db.close();

		return info;
	}

	// 删除阅读题型分类
	public synchronized void delete() {
		db = dbHelper.getWritableDatabase();

		String sql = "delete from " + AppDataBaseHelper.READ_CLASSIFY;
		db.execSQL(sql);
		db.close();
	}

	// 更新阅读题型分类
	public synchronized void update() {
		db = dbHelper.getWritableDatabase();

		String sql = "update " + AppDataBaseHelper.READ_CLASSIFY + " set position= ? ";
		db.execSQL(sql, new Object[] { 1 });
		db.close();
	}

	/**
	 * 插入阅读题型分类
	 * 
	 * @param category
	 */
	public synchronized void insert(ReadClassifyDetail readInfo) {
		db = dbHelper.getWritableDatabase();

		// db.beginTransaction();
		String sql = "insert into " + AppDataBaseHelper.READ_CLASSIFY + " (cn,en,total,position,answer_num,right_num) "
				+ "values(?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { readInfo.cn, readInfo.en, readInfo.total, readInfo.position,
				readInfo.answer_num, readInfo.right_num });
		db.close();

	}

	/**
	 * 更新阅读题型分类
	 * 
	 */
	public synchronized void replaceInto(ReadClassifyDetail readInfo) {
		db = dbHelper.getWritableDatabase();

		String sql = "replace into " + AppDataBaseHelper.READ_CLASSIFY
				+ " (cn,en,total,position,answer_num,right_num) " + "values(?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { readInfo.cn, readInfo.en, readInfo.total, readInfo.position,
				readInfo.answer_num, readInfo.right_num });

		db.close();
	}
}
